<template>
    <div class="bg">
        <div style="background-color: rgba(255,255,255,0.01); display: flex; align-items: center; justify-content: center; width: 25%; padding: 30px 30px; border-radius: 10px">
            <el-form style="width: 100%" ref="formRef" :model="data.form" :rules="data.rules">
                <!--logo-->
                <div style="display: flex; align-items: center; justify-content: center; font-size: 32px">
                    I T C A C P
                </div>
                <!--提示语-->
                <div style="display: flex; align-items: center; justify-content: center; font-size: 20px">
                    <p style="border-bottom: 3px solid #1677ff; padding: 5px 5px; font-size: 14px; color: #1677ff">管理员登录</p>
                </div>
                <!--账号-->
                <el-form-item prop="username">
                    <el-input placeholder="请输入账号" prefix-icon="User" size="default" v-model="data.form.username" autocomplete="off"></el-input>
                </el-form-item>
                <!--密码-->
                <el-form-item prop="password"><!--prop: 属性是用来关联表单验证规则和表单字段-->
                    <el-input show-password placeholder="请输入密码" prefix-icon="Lock" size="default" v-model="data.form.password" autocomplete="off"></el-input>
                </el-form-item>
                <!--按钮-->
                <el-form-item>
                    <el-button type="primary" size="default" style="width: 100%" @click="adminlogin">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>


<script setup>
import {ref, reactive} from 'vue'
import request from "@/utils/request.js";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
const router = useRouter()
const formRef = ref();

//数据
const data = reactive({
    //初始化 form 数据
    form:{
        username: '',
        password: '',
    },
    rules:{
        username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, message: '账号最少3位', trigger: 'blur' },
            { max: 20, message: '账号最多20位', trigger: 'blur' },
            { pattern: /^[a-zA-Z0-9]+$/, message: '账号只能包含字母和数字', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, message: '密码最少6位', trigger: 'blur' },
        ],
    }
})
// 登录逻辑
const adminlogin = () =>{
    formRef.value.validate((valid) => {
        if (valid) {
            try {
                request.post('/api/admin/login', data.form).then(res => {
                    if (res.code === '200') {
                        localStorage.setItem('admin_user', JSON.stringify(res.data || {}))  // 用户对象序列化后并存储
                        ElMessage.success('登录成功')
                        router.push('/manager/managerhome')   //登录成功后跳转到管理员主页
                    } else if (res.code === '500') {
                        ElMessage.error(res.msg)
                    } else {
                        // 处理非200响应（包含后端返回的错误信息）
                        ElMessage.error(`登录失败:${res.msg || '未知错误'}`);
                    }
                })
            }catch (error) {
                ElMessage.error('请求出错:', error);
            }
        }
    })
}
</script>

<!--样式-->
<style>
.bg {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    background-image: url("@/assets/imge/bg.jpg");
    background-size: cover;
}
.el-form-item {
    padding-top: 8px;
}
</style>